<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/11
 * Time: 11:51
 */

namespace Game\Services;


use Game\Library\Mysql;
use Game\Library\Redis;
use Game\Traits\Singleton;
use Predis\Client;
use Workerman\MySQL\Connection;


class ClearService
{
    use Singleton;

    protected $db;
    /** @var Client */
    protected $redis;
    /** @var Connection */
    protected $mysql;
    protected $gameTypeList = [
        26,29,24,25,30,
        2 ,3 ,22,6 ,19,8 ,20,21,
        59,55,52,56,58,53,];
    public function clearRoom($db){
        $this->db = $db;
        $this->mysql = Mysql::getInstance();
        $this->redis = Redis::getInstance($db);
        $roomList = $this->redis->keys('Room:*');

        // 清除房间密码
//        if ($db == 0){
//            $this->clearRoomPassword();
//        }
        foreach ($roomList as $room_str) {

//            echo "Room[".$db."]:". (isset($roomList[0]) ? substr($roomList[0], 5) : "...").PHP_EOL;
//            echo "Count[".$db."]:".count($roomList).PHP_EOL;
            $room_id = substr($room_str, 5);
            $table_name = $this->getRoomTable($db);
            $room_info = $this->mysql->from($table_name)->select("*")->where("room_id=".$room_id)->row();
            // 没有找到房间,直接清除redis
            if (!$room_info ){
//                echo "None Room[".$db."]:".$room_str.PHP_EOL;
//                $keys  = $this->redis->keys("*:".$room_id);
//                echo "Keys[".$db."]:".json_encode($keys);
//                $this->redis->del($keys);

            }elseif ($room_info['is_close'] == 1){
                echo "Closed Room[".$db."]:".$room_str.PHP_EOL;
                $keys  = $this->redis->keys("*:".$room_id);
                echo "Keys[".$db."]:".json_encode($keys);
                $this->redis->del($keys);
            }elseif ($room_info['create_time'] < time() - 24 * 60 * 60 *2){
//                echo "TimeOut Room[".$db."]:".$room_str.PHP_EOL;
//                $keys  = $this->redis->keys("*:".$room_id);
//                echo "Keys[".$db."]:".json_encode($keys);
                //$this->mysql->where("room_id=".$room_id)->set("is_close",1)->update($table_name);
            }
        }

        echo PHP_EOL;
    }


    protected function clearRoomPassword(){
        $list = $this->redis->hgetall("RoomNumber");
        foreach ($list as $password=>$info){
            $data = json_decode($info,true);
            $data["create_timestamp"] = strtotime($data["create_time"]);
            if ($data["create_timestamp"] < time() - 24 * 60 * 60){
                $this->redis->hdel("RoomNumber", $password);
            }
        }
    }

    protected function getRoomTable($gameType){
        $room_table = false;
        switch($gameType)
        {
            case 26: $game_name = "六人炸金花";$room_table = "flower_room"; break;
            case 29: $game_name = "九人炸金花";$room_table = "flower_room"; break;
            case 24: $game_name = "明牌炸金花";$room_table = "flower_room"; break;
            case 25: $game_name = "血拼炸金花";$room_table = "flower_room"; break;
            case 30: $game_name = "十二人金花";$room_table = "flower_room"; break;
            case 2:  $game_name = "斗地主";$room_table = "landlord_room"; break;
            case 3:  $game_name = "三公";$room_table = "sg_room"; break;
            case 22: $game_name = "九人三公";$room_table = "sg_room"; break;
            case 6:  $game_name = "麻将";$room_table = "gd_mahjong_room"; break;
            case 19: $game_name = "九人牌九";$room_table = "paijiu_room"; break;
            case 8:  $game_name = "二八杠";$room_table = "eba_room"; break;
            case 59: $game_name = "九人牛牛";$room_table = "bull_room"; break;
            case 55: $game_name = "六人牛牛";$room_table = "bull_room"; break;
            case 52: $game_name = "十二人牛牛";$room_table = "bull_room"; break;
            case 56: $game_name = "十人牛牛";$room_table = "bull_room"; break;
            case 58: $game_name = "九人八倍牛牛";$room_table = "bull_room"; break;
            case 53: $game_name = "癞子牛牛";$room_table = "laibull_room"; break;
            case 20: $game_name = "大吃小";$room_table = "dcx_room"; break;
            case 21: $game_name = "鱼虾蟹";$room_table = "yxx_room"; break;
            default : $game_name = ""; break;
        }
        return $room_table;
    }

    public function closeRoom(){
        foreach ($this->gameTypeList as $gameType){
            $tableName = $this->getRoomTable($gameType);
            $roomList = $this->mysql->select("*")->from($tableName)->where("is_close = 0")->query();
            foreach ($roomList as $roomInfo){
                if ($roomInfo['create_time'] <= time() - 24*60*60){
                    $this->closeRoomLogic($roomInfo['room_id']);
                }
            }
        }
    }
    protected function closeRoomLogic($room_id){

    }
}